<template>
    <el-drawer 
        v-model="showDrawer" 
        :title="title" 
        :close-on-click-modal="false" 
        :show-close="true"
        @closed="noticeClose"
        :size="size" 
        :destory-on-close="destoryOnClose">
        <div class="formDrawer">
            <div class="body">
                <slot></slot>
            </div>
            <div class="actions">
                <el-button v-if="props.showSubBtn==1 && submitBtn" type="primary" @click="submit" :loading="loading" >{{ confirmText }}</el-button>
                <el-button type="default" @click="close">{{ closeBtnText }}</el-button>
            </div>
        </div>
    </el-drawer>
</template>

<script setup>
import { ref, reactive } from 'vue'
import '@wangeditor/editor/dist/css/style.css'

const props = defineProps({
    title: String,
    //是否显示“提交”按钮，0不显示 1显示，默认1；
    showSubBtn: {
        type: String,
        default: "1",
    },
    size: {
        type: String,
        default: "40%",
    },
    destoryOnClose: {
        type: Boolean,
        default: false,
    },
    confirmText: {
        type: String,
        default: "提交",
    },
    closeBtnText: {
        type: String,
        default: "关闭",
    },
})

const showDrawer = ref(false)
const loading = ref(false)
const submitBtn = ref(true)

const showLoading = () => loading.value = true
const hideLoading = () => loading.value = false
const showSubmitBtn = () => submitBtn.value = true
const hideSubmitBtn = () => submitBtn.value = false
// 打开
const open = () => showDrawer.value = true
// 关闭
const close = () => {
    showDrawer.value = false
}

// 触发通知父组件子组件关闭 
const noticeClose = () => {
    //通知父组件子组件关闭
    isClosed()
}

// 向父组件暴露方法
defineExpose({
    open,
    close,
    showLoading,
    hideLoading,
    showSubmitBtn,
    hideSubmitBtn,
})

// 对外暴露方法
const emit = defineEmits([
    'submit',
    'isClosed',
])

const submit = () => emit("submit")

const isClosed = () => {
    emit("isClosed")
}

// 

</script>

<style>
.formDrawer {
    width: 100%;
    height: 100%;
    position: relative;
    @apply flex flex-col;
}

.formDrawer .body {
    flex: 1;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 50px;
    min-height: 300px;
    overflow-y: auto;
}

.formDrawer .actions {
    height: 50px;
    @apply mt-auto flex items-center;
}
</style>